Imports System.Xml
Imports System.Windows.forms



Public Class FormTagsOptions
    Private xmlText As XmlDocument

    Private Sub FormTagsOptions_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If My.Computer.FileSystem.FileExists(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml") = False Then

                Dim myCreate As CreateXmlFile = New CreateXmlFile
                myCreate.CreateXMLFile()

            End If
            'Dim myform As String = CInt("ddd")
            LoadXML()

            LoadSites()


            txtHTMLTemplate.Text = My.Settings.HtmlTemplate.ToString

        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)

        End Try

    End Sub

    Private Sub LoadXML()
        Try

            xmlText = New XmlDocument
            xmlText.Load(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml")

        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)


        End Try

    End Sub
    Private Sub LoadSites()
        Try

            LoadXML()
            cmbTemplates.Items.Clear()

            Dim nav As XPath.XPathNavigator = xmlText.CreateNavigator
            Dim nodes As XPath.XPathNodeIterator = nav.Select("//site")

            While nodes.MoveNext

                cmbTemplates.Items.Add(nodes.Current.GetAttribute("name", String.Empty))

            End While

            cmbTemplates.SelectedIndex = 0

        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
        End Try

    End Sub

    Private Sub LoadTemplate()

        Try

            LoadXML()


            Dim nav As XPath.XPathNavigator = xmlText.CreateNavigator
            Dim path As XPath.XPathExpression
            Dim node As XPath.XPathNodeIterator

            path = nav.Compile(String.Format("//site[@name='{0}']/tagFormat", cmbTemplates.Text))
            node = nav.Select(path)

            While node.MoveNext
                txtTemplate.Text = node.Current.Value.ToString
            End While

            path = nav.Compile(String.Format("//site[@name='{0}']/inUse", cmbTemplates.Text))
            node = nav.Select(path)
            While node.MoveNext
                If node.Current.Value.ToString = "true" Then
                    chkInUse.Checked = True
                Else
                    chkInUse.Checked = False
                End If
            End While
        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
        End Try


    End Sub

    Private Sub cmbTemplates_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbTemplates.SelectedIndexChanged
        Try

            LoadTemplate()
        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)

        End Try

    End Sub


    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        Try
            AddNewTemplate()

        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)

        End Try
    End Sub

    Private Sub AddNewTemplate()
        Try
            Dim myFormNewTemplate As New FormAddNewTemplate
            myFormNewTemplate.ShowDialog()

            If (myFormNewTemplate.DialogResult = Windows.Forms.DialogResult.OK) Then

                LoadSites()

                cmbTemplates.SelectedIndex = (cmbTemplates.Items.Count - 1)

            End If
        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)

        End Try

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try

            My.Settings.HtmlTemplate = txtHTMLTemplate.Text.Trim.ToString
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
        End Try

    End Sub

    Private Sub btnDeleteThis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteThis.Click
        Try
            Dim myXmlDocument As New XmlDocument

            If Not cmbTemplates.Text Is Nothing Then

                myXmlDocument.Load(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml")
                Dim myXmlNode As XmlNode = Nothing
                myXmlNode = myXmlDocument.SelectSingleNode(String.Format("//site[@name='{0}']", cmbTemplates.Text))
                If Not myXmlNode Is Nothing Then
                    myXmlDocument.DocumentElement.RemoveChild(myXmlNode)
                End If
                myXmlDocument.Save(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml")

            End If

            LoadSites()
        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
        End Try


    End Sub

    Private Sub btnSaveThis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveThis.Click
        SaveCurrent()
    End Sub

    Private Sub SaveCurrent()

        Try
            Dim myXmlDocument As New XmlDocument

            If Not cmbTemplates.Text Is Nothing Then

                myXmlDocument.Load(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml")
                Dim myXmlNode As XmlNode = Nothing
                myXmlNode = myXmlDocument.SelectSingleNode(String.Format("//site[@name='{0}']", cmbTemplates.Text))
                If Not myXmlNode Is Nothing Then

                    myXmlNode("tagFormat").InnerText = txtTemplate.Text

                    If chkInUse.Checked = True Then
                        myXmlNode("inUse").InnerText = "true"
                    ElseIf chkInUse.Checked = False Then
                        myXmlNode("inUse").InnerText = "false"
                    End If
                End If
                myXmlDocument.Save(My.Computer.FileSystem.CurrentDirectory & "\Plugins\TagTemplates.xml")

            End If
        Catch ex As Exception
            Dim mylog As New LogFile
            mylog.WriteToLog(ex.Message.ToString & " " & ex.InnerException.ToString)
        End Try

    End Sub

End Class